<?php
namespace app\user\model;
use app\common\model\Common;
use think\Db;

class Consult extends Common
{	
	protected $name = 'order_consult';
	protected $append = ['shop_name','admin_name','is_stock_text'];

	/**
	 * 获取列表
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param intger $page 当前页数
	 * @param intger $row 每页条数
	 * @param string $search 关键字检索条件
	 */
	public function listData($where, $order, $page, $row, $search = '')
	{
		if($where === false) return [];
		$field = 'oc.*,cg.consult_goods_id,cg.goods_name,cg.is_stock';
		if( $row == 'limit' ){
			return $this->alias('oc')->field($field)
			->join('mps_consult_goods cg','oc.order_consult_id = cg.order_consult_id')
			->where($search)->where($where)->order($order)->group('cg.consult_goods_id')->select();
		}
		return $this->alias('oc')->field($field)
		->join('mps_consult_goods cg','oc.order_consult_id = cg.order_consult_id')
		->where($search)->where($where)->order($order)->group('cg.consult_goods_id')->page($page, $row)->select();
	}
	/**
	 * 获取列表总条数
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function listCount($where, $order, $search = '')
	{
		if($where === false) return 0;
		return $this->alias('oc')
		->join('mps_consult_goods cg','oc.order_consult_id = cg.order_consult_id')
		->where($search)->where($where)->order($order)->count();
	}
	/**
	 * 获取咨询产品
	 * @param  intger $ids 咨询id
	 */
	public function consultGoods($ids)
	{
		$data = Db::name('consultGoods')->where('order_consult_id',$ids)->select();
		foreach($data as $k => $v){
			$data[$k]['is_stock_text'] = $this->is_stock($v['is_stock']);
			$data[$k]['gname'] = $this->goodsName($v);
		}
		return $data;
	}
	/**
	 * 获取咨询图片
	 * @param  intger $ids 咨询id
	 */
	public function consultImg($ids)
	{
		return Db::name('consultImg')->where('order_consult_id',$ids)->column('img_src');
	}	
	/**
	 * 获取器
	 */
	public function getAddtimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}
	public function getShopNameAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('shop')->where('shop_id',$data['shop_id'])->value('shop_name');
	}
	public function getAdminNameAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('admin')->where('admin_id',$data['admin_id'])->value('nkname');
	}
	public function getRemarksAttr($value)
	{
		return $value == '' ? '无' : $value;
	}
	public function getIsStockTextAttr($value, $data)
	{
		switch($data['is_stock']){
			case 0: return '无货';break;
			case 1: return '有货';break;
			default: return '异常';break;
		}
	}

	/*************************** 私有 ************************/
	

	/**
	 * 订单商品状态
	 */
	private function is_stock($is_stock)
	{
		switch($is_stock){
			case 0: return '无货';break;
			case 1: return '有货';break;
			default: return '异常';break;
		}
	}

}